<template>
  <div class="page-container">
    <!-- 搜索筛选区域 -->
    <div class="search-section">
      <el-form :model="queryParams" inline class="search-form" ref="queryRef">
        <el-form-item prop="invoiceType">
          <el-select
            clearable
            v-model="queryParams.invoiceType"
            placeholder="请选择发票类型"
            style="width: 200px"
          >
            <el-option
              v-for="item in bus_invoice_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="invoiceItem">
          <el-select
            clearable
            v-model="queryParams.invoiceItem"
            placeholder="请选择发票项目"
            style="width: 200px"
          >
            <el-option
              v-for="item in bus_invoice_item"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item prop="invoiceItem">
          <el-select
            clearable
            v-model="queryParams.approvalStatus"
            placeholder="请选择审批状态"
            style="width: 200px"
          >
            <el-option
              v-for="item in bus_approval_status"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="开始日期 - 结束日期">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="text" @click="handleSearch"> 查询 </el-button>
          <el-button type="info" link @click="resetSearch"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- <div class="action-section">
        <el-button
          type="primary"
          @click="handleAdd"
          style="background-color: #039691"
        >
          新增
        </el-button>
      </div> -->
    </div>

    <!-- 数据表格 -->
    <div class="table-wrapper" style="margin-top: 10px">
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column type="index" width="80" align="center" label="序号" />
        <el-table-column prop="createTime" label="开票时间" width="160" />
        <el-table-column
          prop="invoiceType"
          label="发票类型"
          width="120"
          align="center"
        >
          <template #default="scope">
            <dict-tag
              :options="bus_invoice_type"
              :value="scope.row.invoiceType"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="invoiceTitle"
          label="开票抬头"
          min-width="180"
          align="center"
        />
        <el-table-column
          prop="invoiceTin"
          label="纳税人识别号"
          min-width="220"
          align="center"
        />
        <el-table-column
          prop="invoiceAmount"
          label="开票金额"
          width="120"
          align="center"
        />
        <el-table-column
          prop="invoiceEmail"
          label="发票接收邮箱"
          width="180"
          align="center"
        />
        <el-table-column prop="" label="开票类型" width="120" align="center">
          <template #default="scope">
            <dict-tag
              :options="bus_invoice_item"
              :value="scope.row.invoiceItem"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="approvalStatus"
          label="开票状态"
          width="100"
          align="center"
        >
          <template #default="scope">
            <dict-tag
              :options="bus_approval_status"
              :value="scope.row.invoiceType"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="recipientTelephone"
          label="收件人+手机号"
          min-width="160"
          align="center"
        />
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="160"
          align="center"
        />
        <el-table-column label="操作" width="180" align="center" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleDetail(row)">
              查看
            </el-button>
            <el-button
              type="danger"
              link
              @click="cancelApply(row)"
              v-if="row.approvalStatus === '2'"
            >
              撤销申请
            </el-button>
            <el-button
              type="danger"
              link
              @click="submitApply(row)"
              v-if="row.approvalStatus === '0'"
              >提交申请
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-section">
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getTableData"
        />
      </div>
    </div>
    <invoicingRecordDetail
      v-model:value="detailVisible"
      :id="id"
      v-if="detailVisible"
    ></invoicingRecordDetail>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, toRefs } from "vue";
import invoicingRecordDetail from "./invoicing-record-detail.vue";

import { getInvoiceLogList, cancelOrApply } from "@/api/invoice.js";

const { proxy } = getCurrentInstance();

const { bus_invoice_type, bus_invoice_item, bus_approval_status } =
  proxy.useDict("bus_invoice_type", "bus_invoice_item", "bus_approval_status");

const dateRange = ref([]);
const loading = ref(false);
const tableData = ref([]);
const total = ref(0);
const detailVisible = ref(false);
const id = ref("");

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    invoiceType: undefined,
    invoiceItem: undefined,
    approvalStatus: undefined,
  },
});

const { queryParams } = toRefs(data);

const handleDetail = (row) => {
  id.value = row.id;
  detailVisible.value = true;
};

function handleSearch() {
  queryParams.pageNum = 1;
  getTableData();
}
function resetSearch() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleSearch();
}

function cancelApply(row) {
  proxy.$modal
    .confirm("是否确认取消申请？")
    .then(function () {
      const params = {
        id: row.id,
        approvalStatus: "0",
      };
      return cancelOrApply(params);
    })
    .then(() => {
      getTableData();
      proxy.$modal.msgSuccess("取消成功");
    })
    .catch(() => {});
}

function submitApply(row) {
  proxy.$modal
    .confirm("是否确认提交申请？")
    .then(function () {
      const params = {
        id: row.id,
        approvalStatus: "2",
      };
      return cancelOrApply(params);
    })
    .then(() => {
      getTableData();
      proxy.$modal.msgSuccess("提交成功");
    })
    .catch(() => {});
}

function getTableData() {
  loading.value = true;
  getInvoiceLogList(proxy.addDateRange(data.queryParams, dateRange.value)).then(
    (res) => {
      loading.value = false;
      tableData.value = res.rows;
      total.value = res.total;
    }
  );
}
onMounted(() => {
  getTableData();
});
</script>

<style lang="scss">
.page-container {
  padding: 10px;
  .content-container {
    display: flex;
    flex-direction: column;
    background-color: #fff;
    .search-wrapper {
      margin: 20px;
      :deep(.el-card__body) {
        padding-bottom: 2px;
      }
    }
  }
}
</style>
